package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.BaseAttrValue;
import com.atguigu.gulimall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gulimall.product.entity.BaseAttrInfo;
import com.atguigu.gulimall.product.service.BaseAttrInfoService;
import com.atguigu.gulimall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nicc
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2022-09-26 18:25:42
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService{

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> getBaseAttrInfoAndValue(Long c1Id,
                                                      Long c2Id,
                                                      Long c3Id) {


        List<BaseAttrInfo> baseAttrInfos = baseAttrInfoMapper.getBaseAttrInfoAndValue(c1Id, c2Id, c3Id);


        return baseAttrInfos;
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1、保存平台属性名
        baseAttrInfoMapper.insert(baseAttrInfo);

        //拿到属性名的自增id
        Long id = baseAttrInfo.getId();

        //2、保存平台属性值
        List<BaseAttrValue> valueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue attrValue : valueList) {
            //给每个属性值回填 属性id
            attrValue.setAttrId(id);
        }
        baseAttrValueService.saveBatch(valueList);
    }

    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1、修改属性名表数据
        baseAttrInfoMapper.updateById(baseAttrInfo);

        //2、修改属性值表数据
        List<BaseAttrValue> valueList = baseAttrInfo.getAttrValueList();

        //带id是修改，不带是新增，

        //3、拿到前端提交的值id集合
        //3.1、先移除所有前端未提交的数据
        List<Long> ids = valueList.stream()
                .filter(item -> item.getId() != null)
                .map(item -> item.getId())
                .collect(Collectors.toList());
        if (ids.size() > 0) {
            //删除前端没带来的属性
            QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id", baseAttrInfo.getId());
            wrapper.notIn("id", ids);
            baseAttrValueService.remove(wrapper);
        } else {
            //全删
            QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id", baseAttrInfo.getId());
            baseAttrValueService.remove(wrapper);
        }


        //3.2、遍历，有值id就修改，没有就新增
        valueList.stream().forEach(item -> {
            if (item.getId() == null) {
                //新增
                item.setAttrId(baseAttrInfo.getId());
                baseAttrValueService.save(item);
            } else {
                //修改
                baseAttrValueService.updateById(item);
            }
        });
    }
}